﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.DotNetty.Common
{
    public class ApplicationConfig
    {
        private static IConfiguration _configuration;
        private static string DefaultConfigFileName="appsetting";
        private static string DefaultConfigFileSuffix= "json";

        public static IConfiguration Configuration
        {
            get {
                if (_configuration ==null)
                {
                    ConfigurationBuilder();
                }
                return _configuration; }
        }

        private static void ConfigurationBuilder(string targetConfig =null)
        {
            string appConfigFile = string.IsNullOrEmpty(targetConfig) ?
            $"{DefaultConfigFileName}.{DefaultConfigFileSuffix}" :
            $"{DefaultConfigFileName}.{targetConfig}.{DefaultConfigFileSuffix}";
            var builder = new ConfigurationBuilder()
                         .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)// 推荐使用Directory.GetCurrentDirectory()
                         .AddJsonFile(appConfigFile);
            _configuration = builder.Build();

        }
        private static ServerConfig _serverConfig;
        /// <summary>
        /// 服务配置
        /// </summary>
        public static ServerConfig ServerConfig => _serverConfig ?? (_serverConfig = new ServerConfig
        {
            Host = Configuration["ServerConfig:Host"],
            Port = Convert.ToInt32(Configuration["ServerConfig:Port"])
        });
    }
}
